//
//  FreshFoodBottomView.swift
//  SmartHousekeeper
//
//  Created by 刘森 on 2020/2/20.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit

class FreshFoodBottomView: UIView {
    
    lazy var shopBagButton: UIButton = {
        
        let value = UIButton(normalImage: ImageName("shopBag"))
        value.setTitle("  购物篮", for: .normal)
        value.setTitleColor(KColorRGBA(110, 110, 110), for: .normal)
        value.titleLabel?.font = KFont(Adapter(14))
        return value
    }()
    lazy var topLine: UIView = {
        let value = UIView()
        value.backgroundColor = KColorRGBA(136, 136, 136)
        return value
    }()
    lazy var paymentButton: UIButton = {
        let value = UIButton()
        value.titleLabel?.numberOfLines = 2
        value.backgroundColor = KColorRGBA(58, 130, 232)
        return value
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubviews([shopBagButton, topLine, paymentButton])
        setPaymetButtonTitle(12.3)
        shopBagButton.lw_showRedDot(12)
    }
    
    override func layoutSubviews() {
        shopBagButton.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(Adapter(30))
        }
        
        topLine.snp.makeConstraints { (make) in
            make.left.top.width.right.equalToSuperview()
            make.height.equalTo(1)
        }
        paymentButton.snp.makeConstraints { (make) in
            make.top.right.bottom.height.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0.44)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setPaymetButtonTitle(_ value: CGFloat) {
        
        let string = String(format: "￥%.2f", value)
        let subText = "立即支付\n" + string
        let attrStr = NSMutableAttributedString(string: subText)
        let style = NSMutableParagraphStyle()
        style.alignment = .center
        style.lineSpacing = Adapter(10)
        let subTextRange = NSMakeRange(0, subText.count)
        attrStr.addAttributes([NSAttributedString.Key.font: KFontBold(Adapter(20))], range: subTextRange)
        attrStr.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], range: subTextRange)
        attrStr.addAttributes([NSAttributedString.Key.paragraphStyle: style], range: subTextRange)
        let range: Range = subText.range(of: string)!
        let location: Int = subText.distance(from: string.startIndex, to: range.lowerBound)
        let value: NSValue = NSValue(range: NSMakeRange(location, string.count))
        attrStr.addAttributes([NSAttributedString.Key.font: KFont(Adapter(15)), NSAttributedString.Key.foregroundColor: UIColor.white], range: value.rangeValue)
        paymentButton.setAttributedTitle(attrStr, for: .normal)
        
    }
    
}
